﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class OfficeInsertViewModel : NotificationObject
    {
        OfficeInsert gWindow;
        OfficeViewModel gParentViewModel;

        public OfficeInsertViewModel(OfficeInsert pWindow, OfficeViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WDistrict> list = WDistrict.GetAll();
            foreach(var item in list)
            {
                DistrictNames.Add(item.DistrictName.Trim());
            }
        }

        private String officeName;
        public String OfficeName
        {
            get
            {
                return officeName;
            }
            set
            {
                officeName = value;
                RaisePropertyChanged("OfficeName");
            }
        }

        private int personnelUID;
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        private List<String> districtNames = new List<String>();
        public List<String> DistrictNames
        {
            get
            {
                return districtNames;
            }

            set
            {
                districtNames = value;
                RaisePropertyChanged("DistrictNames");
            }
        }

        private String districtSelected;
        public String DistrictSelected
        {
            get
            {
                return districtSelected;
            }

            set
            {
                districtSelected = value;
                RaisePropertyChanged("DistrictSelected");
            }
        }

        private String position;
        public String Position
        {
            get
            {
                return position;
            }

            set
            {
                position = value;
                RaisePropertyChanged("Position");
            }
        }

        private String phone;
        public String Phone
        {
            get
            {
                return phone;
            }

            set
            {
                phone = value;
                RaisePropertyChanged("Phone");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WOffice> list = new List<WOffice>();
            WOffice item = new WOffice();
            item.OfficeName = OfficeName;
            item.PersonnelUID = PersonnelUID;
            item.DistrictUID = WDistrict.GetDistrictUID(DistrictSelected);
            item.Position = Position;
            item.Phone = Phone;
            list.Add(item);
            HttpResponse response = WOffice.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
